﻿Imports DataSet_ohlTableAdapters
Imports DataSet_ohl
Imports System.Data

Partial Class Empadres_MatrizValGen
    Inherits System.Web.UI.Page
    Private dataSet As DataSet

    Dim TA_Empadre As New empadreTableAdapter
    Dim DT_Empadre As New empadreDataTable

    Dim TA_EmpMov, TA_tempEM As New Emp_MovsTableAdapter
    Dim DT_EmpMov, DT_tempEM As New Emp_MovsDataTable

    Dim TA_Ubica As New catalogo_generalTableAdapter
    Dim DT_Ubica As New catalogo_generalDataTable

    Dim TA_ValGen As New valor_genTableAdapter
    Dim DT_ValGen As New valor_genDataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim I As Decimal
            For I = Year(Now) To 2006 Step -1
                ddlAno.Items.Add(I)
            Next
            ddlAno.SelectedIndex = 0
            ddlAno_SelectedIndexChanged(ddlAno, e)
        End If

    End Sub

    Protected Sub imgbtnAno_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnAno.Click
        ddlAno.Items.Insert(0, ddlAno.Items(0).Value + 1)
        ddlAno.SelectedIndex = 0
        ddlAno_SelectedIndexChanged(ddlAno, e)
    End Sub

    Protected Sub ddlAno_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAno.SelectedIndexChanged
        lblTitulo.Text = "MATRIZ  VALOR  GENETICO   " & ddlAno.SelectedValue.ToString
        If ddlAno.SelectedIndex = 0 Then
            tbPrincipal = Nothing
        Else
            DT_Empadre = TA_Empadre.GetEmpadreByAno(ddlAno.SelectedValue)
            If DT_Empadre.Rows.Count > 0 Then       'Existen datos en Empadre ?
                Dim i, j As Integer
                Dim xNumCol As Integer = DT_Empadre.Rows.Count

                For i = 0 To 3                      'Recorre renglones
                    Dim tblRenglon As New TableRow()
                    tblRenglon.VerticalAlign = VerticalAlign.Top
                    tblRenglon.BorderStyle = BorderStyle.Inset

                    For j = -1 To xNumCol - 1       'Recorre columnas
                        Dim tblCelda As New TableCell()
                        Dim lbl As New Label
                        lbl.ID = "celda" & i.ToString & j.ToString

                        If i = 0 Then               'Renglon Ubicacion
                            If j = -1 Then          'Columna de encabezados (Ubicacion)
                                lbl.Text = "UBICACION"
                                lbl.ForeColor = Drawing.Color.Maroon    'LightYellow LemonChiffon Wheat
                                'lbl.BackColor = Drawing.Color.LightYellow 'PaleGreen YellowGreen
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)              'PowderBlue SkyBlue - Blue
                            Else
                                Try                 'Checa si ubicacion es Nulo
                                    lbl.Text = DT_Empadre(j).ubicacion
                                Catch ex As Exception
                                    lbl.Text = ""
                                End Try
                                DT_Ubica = TA_Ubica.GetCatGralByTipCve("U", lbl.Text)
                                If DT_Ubica.Rows.Count > 0 Then lbl.Text = DT_Ubica(0).catnom
                                tblCelda.Controls.Add(lbl)
                            End If

                        ElseIf i = 1 Then           'Renglon Clave Semental
                            If j = -1 Then          'Columna de encabezados (Clave Semental)
                                lbl.Text = "SEMENTAL"
                                lbl.ForeColor = Drawing.Color.Maroon
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            Else
                                lbl.Text = DT_Empadre(j).toro
                                lbl.ForeColor = Drawing.Color.Blue
                                lbl.BackColor = Drawing.Color.Wheat
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            End If

                        ElseIf i = 2 Then           'Renglon Nombre Semental
                            If j = -1 Then          'Columna de encabezados (Nombre Semental)
                                lbl.Text = "NOMBRE"
                                lbl.ForeColor = Drawing.Color.Maroon
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            Else
                                lbl.Text = DT_Empadre(j).nombre
                                tblCelda.Controls.Add(lbl)
                            End If

                        ElseIf i = 3 Then           'Renglon Hembras (GRID)
                            If j = -1 Then          'Columna de encabezados (Hembras)
                                lbl.Text = "HEMBRAS"
                                lbl.ForeColor = Drawing.Color.Maroon
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            Else
                                DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(CType(DT_Empadre(j).empadreId, Integer))
                                If DT_EmpMov.Rows.Count > 0 Then        'Existen datos en EmpMovs ?

                                    Dim grdEmp As New GridView
                                    grdEmp.AutoGenerateColumns = False

                                    Dim xbound1, xbound2, xbound3 As New BoundField
                                    xbound1.HeaderText = "VACAS"
                                    xbound2.HeaderText = "AC"
                                    xbound3.HeaderText = "AP"
                                    xbound1.DataField = "Vaca"
                                    xbound2.DataField = "vg_ac"
                                    xbound3.DataField = "vg_ap"
                                    'xbound2.DataFormatString = "{0:d}"
                                    'xbound3.DataFormatString = "{0:d}"
                                    xbound2.ItemStyle.Width = 50
                                    xbound3.ItemStyle.Width = 50
                                    grdEmp.Columns.Add(xbound1)
                                    grdEmp.Columns.Add(xbound2)
                                    grdEmp.Columns.Add(xbound3)

                                    grdEmp.ID = "grdVE" & i.ToString & j.ToString
                                    grdEmp.DataSource = DT_EmpMov
                                    grdEmp.DataBind()

                                    tblCelda.Controls.Add(grdEmp)
                                End If
                            End If
                        Else

                        End If
                        tblCelda.Controls.Add(New LiteralControl())
                        tblRenglon.Cells.Add(tblCelda)
                    Next
                    tbPrincipal.Rows.Add(tblRenglon)

                Next
            End If
            Calcula_ValorGen(ddlAno.SelectedValue)
        End If
    End Sub

    Private Sub Calcula_ValorGen(ByVal xAno As Integer)
        DT_Empadre = TA_Empadre.GetEmpadreByAno(xAno)
        If DT_Empadre.Rows.Count > 0 Then           'Existen datos en Empadre ?
            Dim i, j As Integer

            For i = 0 To DT_Empadre.Rows.Count - 1

                Dim xTac As Decimal = 0.0, xTap As Decimal = 0.0
                'DT_ValGen = TA_ValGen.GetValGenByAnoAni(xAno, CType(DT_Empadre(i).toroId, Integer))
                DT_ValGen = TA_ValGen.GetValGenByAni(CType(DT_Empadre(i).toroId, Integer))
                If DT_ValGen.Rows.Count > 0 Then
                    xTac = DT_ValGen(0).vg_ac
                    xTap = DT_ValGen(0).vg_ap
                End If

                DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(CType(DT_Empadre(i).empadreId, Integer))
                If DT_EmpMov.Rows.Count > 0 Then    'Existen datos en EmpMovs ?

                    For j = 0 To DT_EmpMov.Rows.Count - 1
                        Dim xVac As Decimal = 0.0, xVap As Decimal = 0.0
                        'DT_ValGen = TA_ValGen.GetValGenByAnoAni(xAno, CType(DT_EmpMov(j).vacaId, Integer))
                        DT_ValGen = TA_ValGen.GetValGenByAni(CType(DT_EmpMov(j).vacaId, Integer))
                        If DT_ValGen.Rows.Count > 0 Then
                            xVac = DT_ValGen(0).vg_ac
                            xVap = DT_ValGen(0).vg_ap
                        End If

                        Dim xVG_ac As Decimal = (xTac + xVac) / 2
                        Dim xVG_ap As Decimal = (xTap + xVap) / 2

                        Dim actualizado As Integer
                        'txtSalida.Text = DT_EmpMov(j).empadreId
                        DT_tempEM = TA_tempEM.Get_EM_ByEmpAni(CType(DT_EmpMov(j).empadreId, Integer), CType(DT_EmpMov(j).vacaId, Integer))
                        If DT_tempEM.Rows.Count > 0 Then
                            DT_tempEM(0).vg_ac = xVG_ac
                            DT_tempEM(0).vg_ap = xVG_ap
                            actualizado = TA_tempEM.Update(DT_tempEM)
                        End If

                    Next
                End If
            Next
        End If

    End Sub

End Class
